import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;


public class Biblioteca implements ActionListener {
	static public String arquivo;
	public JButton add = new JButton("add");
	public JButton addPasta = new JButton("addPasta");
	public JButton clear = new JButton("clear");
	public JButton remove = new JButton("remove");
	public JButton addSubLista = new JButton("add SubLista");
	public JButton voltar = new JButton("voltar para pasta raiz");
	public static JList list = new JList();
	public static JScrollPane list_scroll = new JScrollPane(list);
	public static String lista = "raiz";
	public JLabel label = new JLabel("voce esta na lista: " + lista);
	static ArrayList<String> data = new ArrayList<String>();
	public JFrame frame = new JFrame("Biblioteca de " + arquivo);
	public Container content = frame.getContentPane(); 
	public GridBagConstraints c = new GridBagConstraints(); 
	public Biblioteca(){
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		frame.setBounds(30, 30, 500, 500);
		frame.setSize(800,800);
		content.setLayout(new GridBagLayout());		
		add.addActionListener(new Add());
		clear.addActionListener(new Clear());
		remove.addActionListener(new Remove());
		addPasta.addActionListener(new AddPasta());
		addSubLista.addActionListener(new AbreSubLista());
		voltar.addActionListener(new Voltar());
		list.addMouseListener(mouseListener);
		//c.gridwidth = 5;
		c.gridx = 0;
	    c.gridy = 0;
		content.add(label,c);
		c.weightx = 1;
		c.weighty = 1;
		c.gridx = 0;
	    c.gridy = 1;
	    c.fill = GridBagConstraints.BOTH;
		content.add(list_scroll,c);
		list_scroll.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0;
		c.weighty = 0;
		c.gridx = 2;
	    c.gridy = 0;
		content.add(add,c);
		c.gridx = 2;
	    c.gridy = 1;
		content.add(clear,c);
		c.gridx = 2;
	    c.gridy = 2;
		content.add(addPasta,c);
		c.gridx = 3;
	    c.gridy = 2;
		content.add(remove,c);
		c.gridx = 3;
	    c.gridy = 0;
		content.add(addSubLista,c);
		frame.pack();
	}
	public void actionPerformed (ActionEvent e){
		if (e.toString().contains("biblioteca de Filmes")) arquivo = "Filmes";   //Descobre qual butao foi apertado
		if (e.toString().contains("biblioteca de Musicas")) arquivo = "Musicas";
		frame.setTitle("Biblioteca de " + arquivo);
		lista = "raiz";
		Leitor leitor = new Leitor();		
		data.clear();
		for(int i=0;leitor.ler("",arquivo + ".txt")[i]!=null;i++){
			data.add(leitor.ler("",arquivo + ".txt")[i]);
		}
		list.setListData(data.toArray());
		frame.setVisible(true);
	}
	 MouseAdapter mouseListener = new MouseAdapter() {
		 Leitor leitor = new Leitor();
	     public void mouseClicked(MouseEvent j) {
	         if (j.getClickCount() == 2) {
	             if (list.getSelectedValue().toString().contains(">")){
	            	 lista = list.getSelectedValue().toString();
		             data.clear();		  
		     		 for(int i=0;leitor.ler(lista,arquivo + ".txt")[i]!=null;i++){
		     			 data.add(leitor.ler(lista,arquivo + ".txt")[i]);
		     		 }
		     		 list.setListData(data.toArray());
		     		 label.setText("voce esta na lista: " + lista);
		     		c.gridx = 4;
		    	    c.gridy = 1;
		    		content.add(voltar,c);
		    		return;
	             }
	          }
	     }
	 };
	 public class Voltar implements ActionListener {
		 Leitor leitor = new Leitor();
			public void actionPerformed (ActionEvent e){
				lista = "raiz";
				data.clear();		  
	     		 for(int i=0;leitor.ler("",arquivo + ".txt")[i]!=null;i++){
	     			 data.add(leitor.ler("",arquivo + ".txt")[i]);
	     		 }
	     		 list.setListData(data.toArray());
	     		 label.setText("voce esta na lista: " + lista);
	     		 content.remove(voltar);
			}}
	 public class AbreSubLista implements ActionListener {
			public void actionPerformed (ActionEvent e){
				new AddSubLista();
			}}
}
